package com.debao.web.controller.system;

import cn.hutool.core.date.DateUtil;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.utils.DateUtils;
import com.debao.system.domain.Statistic;
import com.debao.system.service.IStatisticService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 首页
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/index")
@CrossOrigin
public class SysIndexController {

    private static final Integer DAY = 7;

    @Resource
    private IStatisticService statisticsService;

    @GetMapping("/statistics")
    public AjaxResult getInfo() {
        HashMap<String, Object> map = new HashMap<>();
        // 前一天的日期
        String beforeDate = DateUtil.formatDate(DateUtil.offsetDay(new Date(), -1));
        // 获取七天前的日期
        List<LocalDate> dayList = DateUtils.getBeforeDayDate(DAY);
        //对dayList 数组倒序
        dayList.sort(Comparator.naturalOrder());
        Statistic statistic = statisticsService.selectStatisticsByDate(beforeDate);

        int[] vipCount = new int[7];
        int[] visitorCount = new int[7];
        int[] coachCount = new int[7];
        // 使用一个索引跟踪循环的顺序
        int index = 0;
        // 根据前七天的日期循环查询统计的数据
        for (LocalDate date : dayList) {
            Statistic data = statisticsService.selectStatisticsByDate(date.toString());
            if (data != null) {
                // 将统计数据插入到数组中
                vipCount[index] = data.getTotalMember();
                visitorCount[index] = data.getTotalSign();
                coachCount[index] = data.getTotalCoach();
            } else {
                // 如果没有数据，可以设置为 0 或者保持默认值
                vipCount[index] = 0;
                visitorCount[index] = 0;
            }
            index++; // 增加索引
        }
        map.put("statistic", statistic);
        map.put("BeforeDateList", dayList);
        map.put("vipCount", vipCount);
        map.put("visitorCount", visitorCount);
        map.put("coachCount", coachCount);
        return AjaxResult.success(map);
    }

    public static void main(String[] args) {

    }
}
